package com.tpfinal.agenda.core.mysqlrequests;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import com.tpfinal.agenda.controller.MainController;
import com.tpfinal.agenda.core.AgendaActionsIDs;
import com.tpfinal.agenda.model.factories.ContactsFactory;

public class GetAllContactsRequest extends MySqlBaseRequest {

	private static final String QUERY = "SELECT * FROM contactos c inner join localidades l on c.cp = l.cp";

	@Override
	public int getID() {
		return AgendaActionsIDs.GET_ALL_CONTACTS;
	}

	@Override
	protected void setup(Map<String, Object> params) {
		this.sqlQuery = QUERY;
		this.queryType = QUERY_TYPE_SELECT;
	}

	@Override
	protected Map<String, Object> processResult(Map<String, Object> resultSet) {
		try {
			ResultSet mysqlResult = (ResultSet) resultSet
					.get(MySqlBaseRequest.MYSQL_RESULTSET);

			MainController.get().setContactsList(
					ContactsFactory
							.createContactListFromMySqlResult(mysqlResult));


			connection.close();
			resultSet.put(RESULT_CODE, OK);
		} catch (SQLException e) {
			e.printStackTrace();
			resultSet.put(RESULT_CODE, ERROR);
		}
		return resultSet;
	}
}
